package com.kuaimacode.kframework.api.controller.invite;

import com.kuaimacode.kframework.api.controller.BaseController;
import com.kuaimacode.kframework.api.service.UserService;
import com.kuaimacode.kframework.api.service.invite.InviteService;
import com.kuaimacode.kframework.common.JsonResult;
import com.kuaimacode.kframework.component.ComponentUtil;
import com.kuaimacode.kframework.exception.ErrorEnum;
import com.kuaimacode.kframework.mybatis.models.invite.Invite;
import com.kuaimacode.kframework.mybatis.models.invite.UserInviteRecord;
import com.kuaimacode.kframework.mybatis.models.user.UserInvite;
import com.kuaimacode.kframework.service.UserInviteService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Optional;

/**
 * @author anzy
 * @date 2019/03/31
 */
@RestController
@RequestMapping("/invite")
@Api(tags = "邀请活动",description = "邀请活动相关api")
public class InviteController extends BaseController {

    @Autowired
    private InviteService inviteService;
    @Autowired
    private UserInviteService userInviteService;

    @Autowired
    private UserService userService;

    @Autowired
    private ComponentUtil componentUtil;

    @ApiOperation(value = "查询最新的邀请活动", notes="查询最新的邀请活动，如：http://118.25.25.228:8886/invite/1?uid=2")
    @GetMapping("/getInviteInfo")
    public JsonResult<Invite> list(){
        Invite invite = inviteService.queryAvailableInvite();
        if (invite ==null){
            invite = inviteService.queryLastInvite();
        }
        if (invite == null) return JsonResult.failure(ErrorEnum.INVITE_NOT_EXIST.getCode(),ErrorEnum.INVITE_NOT_EXIST.getMsg());
        invite.setUrl(invite.getUrl()+getUserId());
        invite.setMiniappUrl(invite.getMiniappUrl()+getUserId());
        Optional.ofNullable(invite)
                .ifPresent(invite_ ->
                        invite_.setShareImg(componentUtil.convertImgAdminUrl(invite_.getShareImg()))
                );

        return JsonResult.success(invite);
    }

    @ApiOperation(value = "查询邀请记录", notes="查询邀请记录")
    @GetMapping("/inviteRecord/{id}")
    @ApiImplicitParam(name = "id", value = "活动ID", required = true, dataType = "long",paramType = "path")
    public JsonResult<UserInviteRecord> inviteRecord(@PathVariable Long id){
        //List<UserInvite> list =  userInviteService.queryByUserId(getUserId(),id);
        List<UserInviteRecord> list = userInviteService.findInviteRecordByUserId(getUserId(),id);
        list.stream().forEach(userInvite -> {
            //处理头像
            Optional.ofNullable(userInvite).ifPresent(o->userInvite.setInviteUserAvatar(componentUtil.convertImgUrl(userInvite.getInviteUserAvatar())));
        });
        return JsonResult.success(list);
    }

    @ApiOperation(value = "查询奖励明细", notes="查询奖励明细")
    @GetMapping("/inviteReward/{id}")
    @ApiImplicitParam(name = "id", value = "活动ID", required = true, dataType = "long",paramType = "path")
    public JsonResult<UserInvite> inviteReward(@PathVariable Long id){
        List<UserInviteRecord> list = userInviteService.findInviteRecordByUserId(getUserId(),id);
        list.stream().forEach(userInvite -> {
            //处理头像
            Optional.ofNullable(userInvite).ifPresent(o->userInvite.setInviteUserAvatar(componentUtil.convertImgUrl(userInvite.getInviteUserAvatar())));
        });
        return JsonResult.success(list);
    }




}
